/*
	Name: 神经网络
	Author: There,hello
	Date: 26-10-19 9:45
	Description: Luogu P1038
	Algorithm: toposort
*/
#include<bits/stdc++.h>
using namespace std;
int n,m,head[10000],tail,val[10000],s[10000],in_[10000],que[10000],que_tail,chu[10000];
#define add(x,y,z) edge[++tail]=(dd){head[x],y,z};head[x]=tail;
struct dd{
    int ne,to,w;
}edge[10000];
void toposort(){
    for(int i=1;i<=n;++i)
        if(!in_[i])
        que[++que_tail]=i;
    for(int i=1;i<=que_tail;++i){
        for(int j=head[que[i]];j;j=edge[j].ne){
            in_[edge[j].to]--;
            if(!in_[edge[j].to])
            que[++que_tail]=edge[j].to;
        }
    }
}
void solve(){
    for(int i=1;i<=n;++i)
        if(!s[i])s[i]=-val[i];
    for(int i=1;i<=que_tail;++i){
        if(s[que[i]]>0)
        for(int j=head[que[i]];j;j=edge[j].ne){
            s[edge[j].to]+=s[que[i]]*edge[j].w;
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        scanf("%d%d",&s[i],&val[i]);
    for(int i=1,x,y,z;i<=m;++i){
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        chu[x]++;
        in_[y]++;
    }
    toposort();
    solve();
    bool f=0;
    for(int i=1;i<=n;++i){
        if(!chu[i]&&s[i]>0){
            f=1;
            printf("%d %d\n",i,s[i]);
        }
    }
    if(!f)printf("NULL");
    return 0;
}